package cn.edu.lzzy.entities;






import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 答题结果
* */

@Entity
public class Result extends BasicEntity{
    public  static final int TYPE_RIGHT=0;
    public  static final int TYPE_MISS=1;
    public  static final int TYPE_EXTRA=2;
    public  static final int TYPE_WRONG=3;
    public  static final int TYPE_UNFINISHED=4;


    private Date commitTime;//提交时间
    @ManyToOne
    private User student;//用户：学生
    @ManyToOne
    private Question question;//题目
    @ManyToMany
    private List<Option> options=new ArrayList<>();

    public Result() {
        this.commitTime = new Date();//将当前时间进行commitTime的初始化
    }

    public Date getCommitTime() {
        return commitTime;
    }

    public void setCommitTime(Date commitTime) {
        this.commitTime = commitTime;
    }
@JsonBackReference
    public User getStudent() {
        return student;
    }

    public void setStudent(User student) {
        this.student = student;
    }
@JsonBackReference
    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    @JsonIgnore
    public List<Option> getOptions() {
        return options;
    }

    public void setOptions(List<Option> options) {
        this.options = options;
    }

    public int what(){
        if(options.size()==0){
            return TYPE_UNFINISHED;
        }
        boolean miss=false;
        boolean extra=false;
        for(Option option: options){
            if(!option.isAnswer()){
                extra=true;
                break;
            }
        }
        for(Option option:question.getOptions()){
            if(!option.isAnswer()){
                if(!options.contains(option)){
                    miss=true;
                    break;
                }
            }
        }
        if(miss && extra){
            return TYPE_WRONG;
        }else if(miss){
            return TYPE_MISS;
        }else if(extra){
            return TYPE_EXTRA;
        }else{
            return TYPE_RIGHT;
        }


    }
}
